<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<?php

/**
 * Read file mainly for the bulk upload process
 */
if ( ! function_exists('tingge_read_file'))
{
    
    /**
     * 
     * @param file path $path
     * @param int $line number of line you will read <br />
     *   -1 will indicate the I will read until end.
     * 
     * @return array a two dimentional "[][]" array
     */
	function tingge_read_file($path, $line=1)
    {
		if (!$fp=@fopen(trim($path), FOPEN_READ)){
			return FALSE;
		}

        
        $return = array();
        if ($line === -1){
            // Infinite Until end
            $i=1;
            while (!feof($fp)) {
                //$line = fread($fp, 1024)."\n";
                //$line = fgets($fp)."\n";
                $row = fgetcsv($fp, 1000, ',', '"');
                if (!$row) break;
                if (!$row[0]) break;
                
                $return[] = $row;
                echo $i.' => '.$line;
                //$return[]=parse_line($line)//
                $i++;
                if ($i>100) break;
            }
        }else{
            for($i=0; $i<$line;$i++){
                //$line = fgets($fp)."\n";
                $row = fgetcsv($fp, 1000, ',', '"');
                if (!$row) break;
                $return[] = $row;
                
            }
        }
		if ($fp) fclose($fp);
        
        return $return;
	}// tingge_read_file
}